/*
 * @(#)Terrain.java     14/08/02
 * 
 * Author             Rick Wilson
 * Copyright (c) 2014 Rick Wilson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of 'Taareekah' nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */



package world;

//~--- non-JDK imports --------------------------------------------------------

import util.Point;

//~--- JDK imports ------------------------------------------------------------

import java.io.File;
import java.io.Serializable;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.management.JMException;

/**
 *
 * @author rick
 */
public class Terrain implements Serializable {

    /** Field description */
    private Map<String, GenericTerrainMap>	terrain_maps;

    /** Field description */
    private BiomeMap	biome_map;

    /** Field description */
    private TerrainTile[]	tiles;

    /** Field description */
    private int	size;

    /**
     *
     * @param size
     */
    public Terrain(int size) {
	terrain_maps	= new HashMap<>();
	this.size	= size;

	try {
	    biome_map	= new BiomeMap(size);
	} catch (Exception ex) {
	    Logger.getLogger(Terrain.class.getName()).log(Level.SEVERE, null, ex);
	}

	createTiles();
    }

    /**
     * Method description
     *
     */
    private void createTiles() {
	tiles	= new TerrainTile[size * size];

	for (int x = 0; x < size; x++) {
	    for (int y = 0; y < size; y++) {

		// x + y * size
		tiles[x + y * size]	= new TerrainTile(x, y);
	    }
	}
    }

    /**
     *
     * @param p
     * @return
     */
    public boolean isValidPoint(Point p) {
	return isValidPoint(p.getX(), p.getY());
    }

    /**
     *
     * @param x
     * @param y
     * @return
     */
    public boolean isValidPoint(int x, int y) {
	if ((x < 0) || (y < 0) || (x >= size) || (y >= size)) {
	    return false;
	}

	return true;
    }

    /**
     *
     * @param x
     * @param y
     * @return
     */
    public TerrainTile getTerrainTile(int x, int y) {
	return tiles[x + y * size];
    }

    /**
     *
     * @param p
     * @return
     */
    public TerrainTile getTerrainTile(Point p) {
	return getTerrainTile(p.getX(), p.getY());
    }

    /**
     *
     * @return
     */
    public int getSize() {
	return size;
    }

    /**
     *
     * @param x
     * @param y
     * @return
     * @throws Exception
     */
    public Map<String, Float> getAllMapValuesAtPoint(int x, int y) throws Exception {
	Map<String, Float>	ret	= new HashMap<>();

	for (Map.Entry<String, GenericTerrainMap> entry : terrain_maps.entrySet()) {
	    ret.put(entry.getKey(), entry.getValue().getValueAtPoint(x, y));
	}

	return ret;
    }

    /**
     *
     * @param map_name
     * @param x
     * @param y
     * @return
     * @throws Exception
     */
    public float getValueOfMapAtPoint(String map_name, int x, int y) throws Exception {
	float	ret	= 0.0f;

	try {
	    ret	= getTerrainMap(map_name).getValueAtPoint(x, y);
	} catch (ClassCastException c) {
	    Logger.getLogger(Terrain.class.getName()).log(Level.SEVERE,
			     "Couldnt cast TerrainMap {0} to type with point {1}, {2}", new Object[] { map_name,
		    x, y });

	    throw c;
	} catch (Exception e) {
	    Logger.getLogger(Terrain.class.getName()).log(Level.SEVERE, "Failed at point {0}, {1}", new Object[] { x,
		    y });

	    throw e;
	}

	return ret;
    }

    /**
     *
     * @param map_name
     * @param p
     * @return
     * @throws Exception
     */
    public float getValueOfMapAtPoint(String map_name, Point p) throws Exception {
	return getValueOfMapAtPoint(map_name, p.getX(), p.getY());
    }

    /**
     *
     * @param prefix
     * @return
     * @throws Exception
     */
    public boolean saveMapsAsRaw(String prefix) throws Exception {
	for (Map.Entry<String, GenericTerrainMap> entry : terrain_maps.entrySet()) {
	    String	filename	= prefix + File.separator + entry.getKey() + ".raw";

	    entry.getValue().saveAsRaw(filename);
	}

	return true;
    }

    /**
     *
     * @param prefix
     * @return
     * @throws Exception
     */
    public boolean saveMapsAsBitmap(String prefix) throws Exception {
	for (Map.Entry<String, GenericTerrainMap> entry : terrain_maps.entrySet()) {
	    String	filename	= prefix + File.separator + entry.getKey() + ".bmp";

	    entry.getValue().saveAsBitmap(filename);
	}

	return true;
    }

    /**
     *
     * @param prefix
     * @return
     * @throws Exception
     */
    public boolean saveMapsAsHumanReadable(String prefix) throws Exception {
	for (Map.Entry<String, GenericTerrainMap> entry : terrain_maps.entrySet()) {
	    String	filename	= prefix + File.separator + entry.getKey() + ".txt";

	    entry.getValue().saveAsHumanReadable(filename);
	}

	return true;
    }

    /**
     *
     * @return
     */
    public Map<String, GenericTerrainMap> getTerrainMaps() {
	return terrain_maps;
    }

    /**
     *
     * @param name
     * @param map
     * @return
     * @throws Exception
     */
    public boolean addTerrainMap(String name, GenericTerrainMap map) throws Exception {
	if (hasTerrainMap(name)) {
	    return false;
	}

	terrain_maps.put(name, map);

	return true;
    }

    /**
     *
     * @param name
     * @return
     * @throws Exception
     */
    public GenericTerrainMap getTerrainMap(String name) throws Exception {
	if (!hasTerrainMap(name)) {
	    throw new JMException("Invalid terrain map");
	}

	return terrain_maps.get(name);
    }

    /**
     *
     * @param name
     * @return
     */
    public boolean hasTerrainMap(String name) {
	return terrain_maps.containsKey(name);
    }

    /**
     *
     * @return
     */
    public BiomeMap getBiomeMap() {
	return biome_map;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
